<?php
/*
 * 手机客户端 精品推荐接口
 * 2013-12-05
 */
include "inc/public_header.php";
include "func/user.php";				//用户信息处理函数

//图片根路径
$IMG_BASEPATH = "http://www.chinalaohu.com/modules/miniature.php?w=180&h=180&p=/uploads/1000*1000/";

$page 	= $_REQUEST["page"];	//分页号 默认页号为0
$asid 	= $_REQUEST["asid"];	//专场主键
$aid	= $_REQUEST["aid"];		//展品信息主键
$order	= $_REQUEST["order"];	//排序类型
$uuid	= $_REQUEST["uuid"];			//会员主键
$reJson = array(); //构建返回值

if(!$aid){
	$aid = "0";
}

//获取用户信息
$uInfoArr = user_getInfoFromUUID();
$uid = "";
if($uInfoArr){
	$uid = $uInfoArr["uid"]; //用户信息主键
}
//echo $uid;

//如果存在展品信息主键，通过展品信息主键获取对应的专场信息主键
if($aid){
	//通过展品信息获取专场ID
	$sql = "select asid from auction where aid='".$aid."'";
	$db->query($sql);
	if($db->next_record()){
		$asid = $db->f("asid");
	}
}
$orderBy = "";	//排序语句段
if($order){
	switch($order){
		case 1:
			$orderBy = "c.asclosetime asc ";
			break;
		case 2:
			$orderBy =  "if(a.lastprice=0,a.startprice,lastprice) desc";
			break;
		case 3:
			$orderBy = "if(a.lastprice = 0,a.startprice,a.lastprice) asc";
			break;
		case 4:
			$orderBy = "a.bids desc";
			break;
		case 5:
			$orderBy = "a.aid desc";
			break;
		default:
			$orderBy = "a.acode asc";
			break;
	}
}

//查询符合条件的展品主键
$sql = "select aid from (select aid,asid,acode,leixin from auction where asid='".$asid."' and atag>0 AND prevtime<=now() order by acode asc) as temp  where asid='".$asid."' and aid not in (".$aid.") group by leixin order by acode asc";
$db->query($sql);
$aids = ""; //展品主键字符串序列
while($db->next_record()){
	if($aids){
		$aids = $aids.",";
	}
	$aids = $aids.$db->f("aid");
}
if(!$aids){
	$aids = "0";
}
//查询推荐信息
$sql = "select
				a.aid as aaid,
				a.atype,
				c.astag as atag,
				a.acode,
				a.auction as aname,
				a.startprice,
				a.oneprice,
				a.lastprice,
				if(a.lastprice = 0,a.startprice,a.lastprice) as showprice,
				date_format(c.asprevtime,'%Y-%m-%d %H:%i:%s') as prevtime,
				date_format(c.asprevclosetime,'%Y-%m-%d %H:%i:%s') as asprevclosetime,
				date_format(c.asopentime,'%Y-%m-%d %H:%i:%s') as starttime,
				date_format(c.asclosetime,'%Y-%m-%d %H:%i:%s') as endtime,
				if(c.asclosetime<=now(),2,if(c.asopentime<=now(),1,0)) as auctiontype

				
		from
				auction a
				inner join auction_special c on a.asid = c.asid
				
		where   
				a.aid in (".$aids.")";
if($orderBy){
	$sql = $sql." ".$orderBy;
}
//获取查询
$reArr = $db->queryArray($sql);

//关注展品主键数组
$gzIds = array();
if($uuid && strlen($uuid)>=32){
	//查我关注的展品主键
	$sql = "
	
	select aid
		from user_concern
		inner join user_base on user_base.uid = user_concern.uid
		where user_base.muuid = '".$uuid."'
	
	";
	//echo $sql;
	$db->query($sql);
	while($db->next_record()){
		$gzIds[] = $db->f("aid");
	}
}

$reJson["success"] = "1";
$reJson["serverTime"] = date('Y-m-d H:i:s');
$list = array();
//echo json_encode($gzIds);
//echo json_encode($reArr);
//循环处理返回记录集
for($i=0;$i<count($reArr);$i++){
	//是否关注了该展品

	if(in_array($reArr[$i]["aaid"],$gzIds)){
		$attention = 1;
	}else{
		$attention = 0;	
	}
	
	$list[] = array(
		"aID"=>$reArr[$i]["aaid"]
		,"auctionCode"=>$reArr[$i]["acode"]
		,"iconUrl"=>$IMG_BASEPATH.$reArr[$i]["acode"]."f1.jpg"
		,"name"=>$reArr[$i]["aname"]
		,"price"=>(string)$reArr[$i]["showprice"]
		,"attention"=>strval($attention)
		,"auctionType"=>(string)$reArr[$i]["auctiontype"]
		,"preSTime"=>(string)$reArr[$i]["prevtime"]
		,"preETime"=>(string)$reArr[$i]["asprevclosetime"]
		,"dealTime"=>(string)$reArr[$i]["endtime"]
		,"lastTime"=>(string)$reArr[$i]["endtime"]
	);
}
$reJson["list"] = $list;
echo json_encode($reJson);
die;
?>